package reduce.fz.com.reduce.fragment;


import android.content.Context;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.alibaba.fastjson.TypeReference;

import java.util.ArrayList;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbRecycleScroollListener;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.adapter.WitnessDetailAdapter;
import reduce.fz.com.reduce.bean.ReplyComment;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.util.HttpUtils;

public class WitnessDetailFragment extends Fragment {
    private RecyclerView mWitnessDetail;
    private SwipeRefreshLayout mWitnessSwipe;
    private WitnessDetailAdapter detailAdapter;
    private Pagination pagination=new Pagination();
    private LinearLayoutManager mLayoutManager;

    private EditText mEtReplyComment;


    public WitnessDetailFragment() {
    }

    public static WitnessDetailFragment create(Long cardid) {
        Bundle bundle = new Bundle();
        if (cardid != null) {
            bundle.putLong("cardid", cardid);
        }
        WitnessDetailFragment fragment = new WitnessDetailFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_witness_detail, container, false);
        initWidget(view);
        initReplyEdit();
        initRecycleConfig(view);
        initLinstener();
        return view;
    }


    public void initReplyEdit()
    {
        mEtReplyComment.setFocusable(true);
        mEtReplyComment.requestFocus();
        InputMethodManager imm = (InputMethodManager) mEtReplyComment.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(mEtReplyComment, InputMethodManager.SHOW_FORCED);
    }


    public void initWidget(View view) {
        mWitnessDetail = (RecyclerView) view.findViewById(R.id.witness_detail);
        mWitnessSwipe = (SwipeRefreshLayout) view.findViewById(R.id.witness_swipe);
        mEtReplyComment=(EditText)view.findViewById(R.id.et_reply_comment);
    }


    public void request(String url) {
       /* HttpUtils.get(url, new AbResponseListener<List<ReplyComment>>(new TypeReference<Result<List<ReplyComment>>>() {
        }) {11
            @Override
            public void responseInfo(Result<List<ReplyComment>> data) {
                if (!data.isSuccess()) return;
                detailAdapter.setFooterNormal();
                detailAdapter.addReplays(data.getData());
            }
        });*/
        List<ReplyComment> comments = new ArrayList<ReplyComment>();
        for (int i = 0; i < 4; i++) {
            comments.add(new ReplyComment());
        }
        detailAdapter.addReplays(comments);


    }

    public void nextPage() {
        pagination.nextPage();
        String url = combUrl(pagination);
        request(url);

    }

    public void firstPage() {
        pagination.reset();
        String url = combUrl(pagination);
        request(url);

    }

    public String combUrl(Pagination pagination) {
        String url = createReqUrl();
        pagination.nextPage();
        url = url + "?pageNo=" + pagination.getPageNo() + "&pageSize=" + pagination.getPageSize();
        return url;
    }

    public String createReqUrl()

    {
        return "";
    }

    private void initRecycleConfig(View view) {
        // 设置ItemAnimator
        mWitnessDetail.setItemAnimator(new DefaultItemAnimator());
        // 设置固定大小
        mWitnessDetail.setHasFixedSize(true);
        detailAdapter = new WitnessDetailAdapter(view.getContext());
        mWitnessDetail.setAdapter(detailAdapter);
    }

    public void initLinstener() {
        mLayoutManager = new LinearLayoutManager(this.getActivity().getBaseContext());
        mWitnessDetail.setLayoutManager(mLayoutManager);
        mWitnessDetail.setOnScrollListener(new AbRecycleScroollListener(mLayoutManager) {
            @Override
            public void onLoadMore() {
                detailAdapter.setFooterLoading();
                nextPage();
                // baseListAdapter.setFooterNormal();

            }
        });
        mWitnessSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                firstPage();
                mWitnessSwipe.setRefreshing(false);

            }
        });

    }
}
